﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cwtrock.model;
using cwtrock.util;

namespace cwtrock.view.handle
{
    public partial class GeneralRptGuide : Form
    {
        private static GeneralRptGuide frm = null;
        private Vouchers voucher_begin = new Vouchers();
        private Vouchers voucher_end = new Vouchers();
        private Vrecords vrecord_begin = new Vrecords();
        private Vrecords vrecord_end = new Vrecords();
         // 总账 明细账的会计期间
        private string duration;


        #region 单体模式
        /// <summary>
        /// 单体模式
        /// </summary>
        /// <returns></returns>
        public static GeneralRptGuide getInstance()
        {
           
            if (frm == null)
            {
                frm = new GeneralRptGuide();

            }

            return frm;
        }
        #endregion


        public GeneralRptGuide()
        {
            InitializeComponent();
            ok_btn.Select();

            initFrm();

            SysUsr currentUser = AppContextProvider.instance.LogUser;
            voucher_begin.Vmakerid = currentUser.Usrid;
            vrecord_begin.Vmakerid = currentUser.Usrid;
        }

        private void initFrm() {

           int month =  DateTime.Now.Date.Month;
           int year = DateTime.Now.Date.Year;


           duration_begin.Value = month;
           duration_end.Value = month;

           writeDuration(month, month);
        
        }

        private void duration_begin_ValueChanged(object sender, EventArgs e)
        {
            
            int month_begin =(int)duration_begin.Value;
            int month_end = (int)duration_end.Value;
            duration_end.Minimum = month_begin;

            writeDuration(month_begin, month_end);
        }

 

        private void duration_end_ValueChanged(object sender, EventArgs e)
        {
            int month_begin = (int)duration_begin.Value;
            int month_end = (int)duration_end.Value;
            duration_begin.Maximum = month_end;

            writeDuration(month_begin, month_end);
        }

        // 写日期范围
        private void writeDuration(int month_begin, int month_end)
        {

           int cur_year = DateTime.Now.Date.Year;

            DateTime first = new DateTime(cur_year, month_begin, 1);
            DateTime last = new DateTime(cur_year, month_end, DateTime.DaysInMonth(cur_year, month_end));

            int firstday = first.Day;
            int lastday =  last.Day;

            voucher_begin.Vdate = first;
            voucher_end.Vdate = last;

            duration_txt.Text = cur_year + "年" + month_begin + "月" + firstday + "日至" + month_end + "月" + lastday + "日";
            if (month_begin == month_end)
                duration = cur_year + "年" + month_begin + "月";
            else
                duration = cur_year+"年"+ + month_begin + "月至" + month_end+"月";
            
        }

        private void acct_tree_btn_Click(object sender, EventArgs e)
        {
            AccountsMainFrm acctfrm = AccountsMainFrm.getInstance("确定");
            acctfrm.Selflag = "0";
            DialogResult rlt = acctfrm.ShowDialog();
            if (rlt == DialogResult.OK)
            {
                string acctcode = acctfrm.Selacctcode;
                acct_begin.Text = acctcode;
                vrecord_begin.Acctcode = acctcode;

            }
        }

        private void acct_tree_btn1_Click(object sender, EventArgs e)
        {
            AccountsMainFrm acctfrm = AccountsMainFrm.getInstance("确定");
            acctfrm.Selflag = "1";
            DialogResult rlt = acctfrm.ShowDialog();
            if (rlt == DialogResult.OK)
            {
                string acctcode = acctfrm.Selacctcode;
                acct_end.Text = acctcode;
                vrecord_end.Acctcode = acctcode;
            }
        }

        private void ok_btn_Click(object sender, EventArgs e)
        {
            GeneralRpt frm = GeneralRpt.getInstance();
            FrmMain mainfrm = FrmMain.getInstance();
            frm.MdiParent = mainfrm;
            frm.Voucher_begin = voucher_begin;
            frm.Voucher_end = voucher_end;
            frm.Vrecord_begin = vrecord_begin;
            frm.Vrecord_end = vrecord_end;
            frm.writeDuration(duration);
            frm.Show();
            frm.initGeneralRptData();         
            this.Close();
        }

        public void writeAcctQueryFrom(string acctcode) {
            acct_begin.Text = acctcode;
            vrecord_begin.Acctcode = acctcode;
        
        }

        public void writeAcctQueryEnd(string acctcode)
        {
            acct_end.Text = acctcode;
            vrecord_end.Acctcode = acctcode;

        }

        private void acct_begin_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

          

            if ((int)e.KeyChar <= 32)  // 特殊键(含空格), 不处理
            {
                return;
            }

            if (!char.IsDigit(e.KeyChar))  // 非数字键, 放弃该输入
            {
                e.Handled = true;
                return;
            }
        }

        private void acct_begin_Leave(object sender, EventArgs e)
        {
          
            vrecord_begin.Acctcode =  acct_begin.Text ;
        }

        private void acct_end_Leave(object sender, EventArgs e)
        {
            vrecord_end.Acctcode = acct_end.Text;
        }
    }
}
